<?php
namespace app\index\controller;

use app\index\model\AssetModel;
use app\index\model\DangJieListModel;
use app\index\model\DangJieModel;
use app\index\model\DangModel;
use app\index\model\DeptModel;
use app\index\model\EmailInfoModel;
use app\index\model\EmailModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\MeetModel;
use app\index\model\NewsModel;
use app\index\model\NoticeModel;
use app\index\model\OptionModel;
use app\index\model\SealGaiListModel;
use app\index\model\SealGaiModel;
use app\index\model\SealModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;
use app\index\model\WenBanModel;
use app\index\model\WenFaModel;
use app\index\model\WenFenModel;
use app\index\model\WenShouModel;
use app\index\model\WuCaiListModel;
use app\index\model\WuCaiModel;
use app\index\model\WuChuModel;
use app\index\model\WuLingListModel;
use app\index\model\WuLingModel;
use app\index\model\WuRuListModel;
use app\index\model\WuRuModel;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use app\index\service\WorkerService;
use app\index\service\XingService;
use think\Db;
use think\Request;

class Xing extends Common
{
    public function list_notice(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['notice.deleted_user','=', 0];
        $where[] = ['notice.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['notice.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['notice.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['tongtype']) && !empty($param['tongtype'])){
            $where[] = ['notice.tongtype','=', $param['tongtype']];
        }


        if(isset($param['tongtype']) && !empty($param['tongtype'])){
            $where[] = ['notice.tongtype','=', $param['tongtype']];
        }
        if(isset($param['facom']) && !empty($param['facom'])){
            $where[] = ['notice.facom','=', $param['facom']];
        }
        if(isset($param['fadt']) && !empty($param['fadt'])){
            $where[] = ['notice.fadt','between', explode(' - ', $param['fadt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['notice.status','=', $param['status']];
        }

        $data['facom'] = (new UserService())->get_deptinfo_all();
        $data['is_facom'] = isset($param['facom']) ? $param['facom'] : '';

        $data['tongtype'] = (new XingService())->get_notice_tongtype();
        $data['is_tongtype'] = isset($param['tongtype']) ? $param['tongtype'] : '';


        $data['data'] = NoticeModel::alias('notice')
            ->field('notice.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('notice', 'notice'))
            ->order('notice.ding DESC,notice.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['tongtype'] = (new XingService())->get_notice_tongtype_show($item['tongtype']);
                $item['jietype'] = (new XingService())->get_notice_jietype_show($item['jietype']);
                $item['facom_name'] = DeptModel::get_name_by_pk($item['facom']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('notice');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_noticemy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['notice.deleted_user','=', 0];
        $where[] = ['notice.comid','=',$this->userinfo['comid']];
        $where[] = ['notice.status','=',1];

        $wherejieuser = "find_in_set(".$this->userinfo['id'].",jieuser) or jieuser='' or jieuser is null";

        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['notice.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['tongtype']) && !empty($param['tongtype'])){
            $where[] = ['notice.tongtype','=', $param['tongtype']];
        }

        if(isset($param['facom']) && !empty($param['facom'])){
            $where[] = ['notice.facom','=', $param['facom']];
        }
        if(isset($param['fadt']) && !empty($param['fadt'])){
            $where[] = ['notice.fadt','between', explode(' - ', $param['fadt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['notice.status','=', $param['status']];
        }


        $data['facom'] = (new UserService())->get_deptinfo_all();
        $data['is_facom'] = isset($param['facom']) ? $param['facom'] : '';

        $data['tongtype'] = (new XingService())->get_notice_tongtype();
        $data['is_tongtype'] = isset($param['tongtype']) ? $param['tongtype'] : '';


        $data['data'] = NoticeModel::alias('notice')
            ->field('notice.*')
            ->where($where)
            ->where($wherejieuser)
            ->order('notice.ding DESC,notice.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['tongtype'] = (new XingService())->get_notice_tongtype_show($item['tongtype']);
                $item['jietype'] = (new XingService())->get_notice_jietype_show($item['jietype']);
                $item['facom_name'] = DeptModel::get_name_by_pk($item['facom']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_notice(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['tongtype'])){
                return outputJson(4000, '请选择通知类型');
            }
            if(empty($param['jietype'])){
                return outputJson(4000, '请选择接收范围');
            }else{
                if($param['jietype'] != 'all'){
                    if(empty($param['jieer'])){
                        return outputJson(4000, '请选择接收范围的范围值');
                    }
                }
            }

            $param['facom'] = $this->userinfo['deptid'];

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                if($param['status'] == 1){
                    $jieuser = (new XingService())->get_notice_jieer_by_jietype($param['jietype'], $param['jieer']);
                    $param['jieuser'] = $jieuser;
                    $param['fadt'] = date('Y-m-d H:i:s');
                }




                $id = NoticeModel::insertGetId($param);

                if($param['status'] == 1){
                    $facomName = DeptModel::get_name_by_pk($param['facom']);
                    $tongtype = (new XingService())->get_notice_tongtype_show($param['tongtype']);
                    $mess = "[".$facomName."]发布了[".$tongtype."][".$param['title']."]，请查看!";
                    (new OptService())->add_todo($jieuser, '通知公告',$mess, $id, 'notice');
                }

                (new OptService())->relation_file($files,'notice',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'notice', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'notice', $id, '通知公告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['facom_name'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['tongtype'] = (new XingService())->get_notice_tongtype();
            $data['jietype'] = (new XingService())->get_notice_jietype();
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['roleArr'] = (new UserService())->get_user_roleArr();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_notice(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = NoticeModel::where(['id'=>$param['id']])->find();

            if($old['status'] != 0){
                return outputJson(4000, '该公告信息已发布,不能编辑');
            }

            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['tongtype'])){
                return outputJson(4000, '请选择通知类型');
            }
            if(empty($param['jietype'])){
                return outputJson(4000, '请选择接收范围');
            }else{
                if($param['jietype'] != 'all'){
                    if(empty($param['jieer'])){
                        return outputJson(4000, '请选择接收范围的范围值');
                    }
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                if($param['status'] == 1){
                    $jieuser = (new XingService())->get_notice_jieer_by_jietype($param['jietype'], $param['jieer']);
                    $param['jieuser'] = $jieuser;
                    $param['fadt'] = date('Y-m-d H:i:s');
                }

                NoticeModel::update($param);

                if($param['status'] == 1){
                    $facomName = DeptModel::get_name_by_pk($old['facom']);
                    $tongtype = (new XingService())->get_notice_tongtype_show($param['tongtype']);
                    $mess = "[".$facomName."]发布了[".$tongtype."][".$param['title']."]，请查看!";
                    (new OptService())->add_todo($jieuser, '通知公告',$mess, $param['id'], 'notice');
                }

                (new OptService())->relation_file($files,'notice',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'notice', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'notice', $param['id'], '通知公告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = NoticeModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['facom_name'] = DeptModel::get_name_by_pk($data['data']['facom']);

            if($data['data']['jietype'] == 'dept'){
                $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'dept');
            }
            if($data['data']['jietype'] == 'gang'){
                $data['data']['gangid'] = explode(',', $data['data']['jieer']);
            }
            if($data['data']['jietype'] == 'role'){
                $data['data']['roleid'] = explode(',', $data['data']['jieer']);
            }
            if($data['data']['jietype'] == 'user'){
                $data['data']['userid_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');
            }


            $data['tongtype'] = (new XingService())->get_notice_tongtype();
            $data['jietype'] = (new XingService())->get_notice_jietype();
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['roleArr'] = (new UserService())->get_user_roleArr();
            $data['files'] = (new OptService())->get_file('notice', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function ding_notice(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = NoticeModel::where(['id'=>$param['id']])->find();

            $ding = 0;
            if($old['ding'] < 1){
                $max = NoticeModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->max('ding');
                $ding = $max + 1;
            }


            Db::startTrans();
            try {
                NoticeModel::where(['id'=>$old['id']])->update(['ding'=>$ding]);
                (new OptService())->add_flow_log('置顶', 'notice', $param['id']);
                (new OptService())->add_log_list('置顶', 'notice', $param['id'], '通知公告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function fei_notice(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = NoticeModel::where(['id'=>$param['id']])->find();

            if($old['status'] != 1){
                return outputJson(4000, '操作失败');
            }

            Db::startTrans();
            try {
                NoticeModel::where(['id'=>$old['id']])->update(['status'=>2]);
                (new OptService())->add_flow_log('作废', 'notice', $param['id']);
                (new OptService())->add_log_list('作废', 'notice', $param['id'], '通知公告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function del_notice(Request $request){
        $param = $request->post();
        $data = NoticeModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            NoticeModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'notice', $data['id'], '通知公告');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_notice(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = NoticeModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        if($data['data']['jietype'] == 'dept'){
            $jieer = DeptModel::where('id','in',$data['data']['jieer'])->column('name');
            $data['data']['jieer'] = implode(',', $jieer);
        }
        if($data['data']['jietype'] == 'gang'){
            $jieer = UserGangModel::where('id','in',$data['data']['jieer'])->column('name');
            $data['data']['jieer'] = implode(',', $jieer);
        }
        if($data['data']['jietype'] == 'role'){
            $jieer = UserRoleModel::where('id','in',$data['data']['jieer'])->column('name');
            $data['data']['jieer'] = implode(',', $jieer);
        }
        if($data['data']['jietype'] == 'user'){
            $jieer = UserModel::where('id','in',$data['data']['jieer'])->column('realname');
            $data['data']['jieer'] = implode(',', $jieer);
        }

        $data['data']['tongtype'] = (new XingService())->get_notice_tongtype_show($data['data']['tongtype']);
        $data['data']['jietype'] = (new XingService())->get_notice_jietype_show($data['data']['jietype']);
        $data['data']['status_name'] = (new XingService())->get_notice_status_show($data['data']['status']);
        $data['data']['facom_name'] = DeptModel::get_name_by_pk($data['data']['facom']);


        $data['files'] = (new OptService())->get_file('notice', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_email_nav(Request $request){
        $shou = EmailModel::where(['comid'=>$this->userinfo['comid'],'recid'=>$this->userinfo['id'],'reciddel'=>0])->count();
        $shouNo = EmailModel::where(['comid'=>$this->userinfo['comid'],'recid'=>$this->userinfo['id'],'reciddel'=>0,'isread'=>0])->count();
        $cao = EmailModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id'],'uiddel'=>0,'recid'=>0])->count();
        $fa = EmailModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id'],'uiddel'=>0])
            ->where('recid','gt',0)->group('infoid')->count();
        $shan = EmailModel::where(['comid'=>$this->userinfo['comid']])
            ->where("(uid=".$this->userinfo['id']." and uiddel=1) or (recid=".$this->userinfo['id']." and reciddel=1)")
            ->group('infoid')->count();

        $data = [
            ['id'=>1, 'title'=>'收件箱（'.$shouNo.'/'.$shou.'）'],
            ['id'=>2, 'title'=>'草稿箱（'.$cao.'）'],
            ['id'=>3, 'title'=>'已发送（'.$fa.'）'],
            ['id'=>4, 'title'=>'已删除（'.$shan.'）'],
        ];
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_email(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['email.comid','=',$this->userinfo['comid']];

        $param['type'] = isset($param['type']) ? $param['type'] : 0;

        $where2 = "";
        $group = "";
        if($param['type'] == 1){ //收件
            $where[] = ['email.recid','=',$this->userinfo['id']];
            $where[] = ['email.reciddel','=',0];
        }else if($param['type'] == 2){ //草稿
            $where[] = ['email.uid','=',$this->userinfo['id']];
            $where[] = ['email.uiddel','=',0];
            $where[] = ['email.recid','=',0];
        }else if($param['type'] == 3){ //已发送
            $where[] = ['email.uid','=',$this->userinfo['id']];
            $where[] = ['email.uiddel','=',0];
            $where[] = ['email.recid','gt',0];
            $group = "email.infoid";
        }else if($param['type'] == 4){ //已删除
            $where2 = "email.uiddel=".$this->userinfo['id']." or email.reciddel=".$this->userinfo['id'];
            $group = "email.infoid";
        }else{
            $where[] = ['email.id','lt',0];
        }


        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['info.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['uid_name']) && !empty($param['uid_name'])){
            $where[] = ['user.realname','like', '%'.$param['uid_name'].'%'];
        }
        if(isset($param['fadt']) && !empty($param['fadt'])){
            $where[] = ['email.fadt','between', explode(' - ', $param['fadt'])];
        }

        $geiWhere = '';
        if(isset($param['recid_name']) && !empty($param['recid_name'])){
            $getId = UserModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where('realname','like','%'.$param['recid_name'].'%')->column('id');
            foreach ($getId as $gid){
                if(empty($geiWhere)){
                    $geiWhere = "find_in_set(".$gid.",faer)";
                }else{
                    $geiWhere .= " or find_in_set(".$gid.",faer)";
                }
            }
            if(empty($geiWhere)){
                $where[] = ['email.id','lt', 0];
            }
        }


        $data['data'] = EmailModel::alias('email')
            ->field('email.*,user.realname as uid_name,info.title,info.faer,info.chaoer')
            ->join('email_info info','info.id=email.infoid')
            ->leftjoin('user user','user.id=email.uid')
            ->where($where)
            ->where($where2)
            ->where($geiWhere)
            ->group($group)
            ->order('email.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['faer_name'] = UserModel::get_realname_by_ids($item['faer']);
                $item['chaoer_name'] = UserModel::get_realname_by_ids($item['chaoer']);

                $item['reply'] = false;
                if($item['recid'] == $this->userinfo['id'] && $item['reciddel'] == 0){
                    $item['reply'] = true;
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_email(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['title'])){
                return outputJson(4000, '请输入主题');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请选择发送给');
            }

            $faerArr = explode(',', $param['faer']);
            $chaoerArr = explode(',', $param['chaoer']);
            if(in_array($this->userinfo['id'], $faerArr)){
                return outputJson(4000, '不能发送给自己');
            }
            if(in_array($this->userinfo['id'], $chaoerArr)){
                return outputJson(4000, '不能抄送给自己');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);





            $info = [];
            $info['title'] = $param['title'];
            $info['faer'] = $param['faer'];
            $info['chaoer'] = $param['chaoer'];
            $info['explain'] = $param['explain'];

            Db::startTrans();
            try {
                $infoid = EmailInfoModel::insertGetId($info);

                if($param['status'] > 0){
                    $duier = array_unique(array_merge($faerArr, $chaoerArr));
                    foreach ($duier as $ren){
                        if(empty($ren)){
                            continue;
                        }
                        $emailid = EmailModel::insertGetId([
                            'comid' => $param['comid'],
                            'infoid' => $infoid,
                            'uid' => $this->userinfo['id'],
                            'recid' => $ren,
                            'fadt' => $param['created_at'],
                            'created_user' => $param['created_user'],
                            'updated_user' => $param['updated_user'],
                            'created_at' => $param['created_at'],
                            'updated_at' => $param['updated_at'],
                        ]);
                        $mess = "[".$this->userinfo['realname']."]：[".$info['title']."]，请查看！";
                        (new WorkerService())->add_worker_email($ren, $mess, $emailid, 'email');
                    }
                }else{
                    EmailModel::insert([
                        'comid' => $param['comid'],
                        'infoid' => $infoid,
                        'uid' => $this->userinfo['id'],
                        'created_user' => $param['created_user'],
                        'updated_user' => $param['updated_user'],
                        'created_at' => $param['created_at'],
                        'updated_at' => $param['updated_at'],
                    ]);
                }

                (new OptService())->relation_file($files,'email_info',$infoid);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'email', $infoid);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'email', $infoid, '邮件');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'), []);
        }
    }
    public function edit_email(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EmailModel::where(['id'=>$param['id']])->find();
            $oldInfo = EmailInfoModel::where(['id'=>$old['infoid']])->find();

            if(empty($param['title'])){
                return outputJson(4000, '请输入主题');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请选择发送给');
            }

            $faerArr = explode(',', $param['faer']);
            $chaoerArr = explode(',', $param['chaoer']);
            if(in_array($this->userinfo['id'], $faerArr)){
                return outputJson(4000, '不能发送给自己');
            }
            if(in_array($this->userinfo['id'], $chaoerArr)){
                return outputJson(4000, '不能抄送给自己');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $info = [];
            $info['id'] = $oldInfo['id'];
            $info['title'] = $param['title'];
            $info['faer'] = $param['faer'];
            $info['chaoer'] = $param['chaoer'];
            $info['explain'] = $param['explain'];

            Db::startTrans();
            try {
                EmailInfoModel::update($info);

                if($param['status'] > 0){
                    EmailModel::where(['id'=>$old['id']])->delete();
                    $duier = array_unique(array_merge($faerArr, $chaoerArr));
                    foreach ($duier as $ren){
                        if(empty($ren)){
                            continue;
                        }
                        $emailid = EmailModel::insertGetId([
                            'comid' => $this->userinfo['comid'],
                            'infoid' => $info['id'],
                            'uid' => $this->userinfo['id'],
                            'recid' => $ren,
                            'fadt' => $param['updated_at'],
                            'created_user' => $param['updated_user'],
                            'updated_user' => $param['updated_user'],
                            'created_at' => $param['updated_at'],
                            'updated_at' => $param['updated_at'],
                        ]);
                        $mess = "[".$this->userinfo['realname']."]：[".$info['title']."]，请查看！";
                        (new WorkerService())->add_worker_email($ren, $mess, $emailid, 'email');
                    }
                }


                (new OptService())->relation_file($files,'email_info',$info['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'email', $info['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'email', $info['id'], '邮件');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $email = EmailModel::where(['id'=>$id])->find();
            $data['data'] = EmailInfoModel::where(['id'=>$email['infoid']])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['data']['faer_info'] = (new UserService())->get_users_part_arr($data['data']['faer'], 'user');
            $data['data']['chaoer_info'] = (new UserService())->get_users_part_arr($data['data']['chaoer'], 'user');

            $data['files'] = (new OptService())->get_file('email_info', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function reply_email(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['title'])){
                return outputJson(4000, '请输入主题');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请选择发送给');
            }

            $faerArr = explode(',', $param['faer']);
            $chaoerArr = explode(',', $param['chaoer']);
            if(in_array($this->userinfo['id'], $faerArr)){
                return outputJson(4000, '不能发送给自己');
            }
            if(in_array($this->userinfo['id'], $chaoerArr)){
                return outputJson(4000, '不能抄送给自己');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);





            $info = [];
            $info['title'] = $param['title'];
            $info['faer'] = $param['faer'];
            $info['chaoer'] = $param['chaoer'];
            $info['explain'] = $param['explain'];

            Db::startTrans();
            try {
                $infoid = EmailInfoModel::insertGetId($info);

                if($param['status'] > 0){
                    $duier = array_unique(array_merge($faerArr, $chaoerArr));
                    foreach ($duier as $ren){
                        if(empty($ren)){
                            continue;
                        }
                        $emailid = EmailModel::insertGetId([
                            'comid' => $param['comid'],
                            'infoid' => $infoid,
                            'uid' => $this->userinfo['id'],
                            'recid' => $ren,
                            'fadt' => $param['created_at'],
                            'created_user' => $param['created_user'],
                            'updated_user' => $param['updated_user'],
                            'created_at' => $param['created_at'],
                            'updated_at' => $param['updated_at'],
                        ]);
                        $mess = "[".$this->userinfo['realname']."]：[".$info['title']."]，请查看！";
                        (new WorkerService())->add_worker_email($ren, $mess, $emailid, 'email');
                    }
                }else{
                    EmailModel::insert([
                        'comid' => $param['comid'],
                        'infoid' => $infoid,
                        'uid' => $this->userinfo['id'],
                        'created_user' => $param['created_user'],
                        'updated_user' => $param['updated_user'],
                        'created_at' => $param['created_at'],
                        'updated_at' => $param['updated_at'],
                    ]);
                }

                (new OptService())->relation_file($files,'email_info',$infoid);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'email', $infoid);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'email', $infoid, '邮件');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $email = EmailModel::where(['id'=>$id])->find();
            $data['data'] = EmailInfoModel::where(['id'=>$email['infoid']])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['title'] = 'Re：'.$data['data']['title'];

            $data['data']['explain'] = '

--------------以下是原始邮件--------------
'.$data['data']['explain'];
            $data['data']['faer'] = $email['uid'];


            $data['data']['faer_info'] = (new UserService())->get_users_part_arr($data['data']['faer'], 'user');
            $data['data']['chaoer_info'] = (new UserService())->get_users_part_arr($data['data']['chaoer'], 'user');

            $data['files'] = (new OptService())->get_file('email_info', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_email(Request $request){
        $param = $request->post();
        $data = EmailModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {

            if($data['uid'] == $this->userinfo['id']){
                if($data['uiddel'] < 1){
                    EmailModel::where(['uid'=>$this->userinfo['id'],'infoid'=>$data['infoid']])->update(['uiddel'=>$this->userinfo['id']]);
                }else{
                    EmailModel::where(['uid'=>$this->userinfo['id'],'infoid'=>$data['infoid']])->delete();
                }
            }

            if($data['recid'] == $this->userinfo['id']){
                if($data['reciddel'] < 1){
                    EmailModel::where(['id'=>$data['id']])->update(['reciddel'=>$this->userinfo['id']]);
                }else{
                    EmailModel::where(['id'=>$data['id']])->delete();
                }
            }

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'email', $data['id'], '邮件');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_email(Request $request){
        $id = $request->post('id', 0, 'int');
        $email = EmailModel::where(['id'=>$id])->find();
        $data['data'] = EmailInfoModel::where(['id'=>$email['infoid']])->find();
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $data['data']['fadt'] = $email['fadt'];
        $data['data']['uid_name'] = UserModel::get_realname_by_pk($email['uid']);
        $data['data']['faer_name'] = UserModel::get_realname_by_ids($data['data']['faer']);
        $data['data']['chaoer_name'] = UserModel::get_realname_by_ids($data['data']['chaoer']);

        if($email['isread'] < 1){
            if($email['recid'] == $this->userinfo['id']){
                EmailModel::where(['id'=>$email['id']])->update(['isread'=>1]);
            }
        }

        $data['files'] = (new OptService())->get_file('email_info', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }




    public function list_wu_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.flowuser|cai.flowdept|cai.number|cai.title|cai.gonger|cai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['cai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['cai.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['cai.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['cai.status','=', $param['status']];
        }

        $data['data'] = WuCaiModel::alias('cai')
            ->field('cai.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wu_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wu_cai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['flowuser'])){
                return outputJson(4000, '请输入申请人');
            }
            if(empty($param['flowdept'])){
                return outputJson(4000, '请输入申购部门');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入采购主题');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入供应方');
            }
            if(empty($param['gongtel'])){
                return outputJson(4000, '请输入供应方联系方式');
            }



            $param['number'] = (new XingService())->create_wu_cai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入采购物品明细');
            }


            Db::startTrans();
            try {
                $id = WuCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    WuCaiListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'wu_cai',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_cai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_cai', $id, '物品采购');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['number'] = (new XingService())->create_wu_cai_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WuCaiModel::where(['id'=>$param['id']])->find();

            if(empty($param['flowuser'])){
                return outputJson(4000, '请输入申请人');
            }
            if(empty($param['flowdept'])){
                return outputJson(4000, '请输入申购部门');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入采购主题');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入供应方');
            }
            if(empty($param['gongtel'])){
                return outputJson(4000, '请输入供应方联系方式');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入采购物品明细');
            }

            Db::startTrans();
            try {
                WuCaiModel::update($param);

                WuCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    WuCaiListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'wu_cai',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_cai', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_cai', $param['id'], '物品采购');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_cai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wu_cai(Request $request){
        $param = $request->post();
        $data = WuCaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            WuCaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wu_cai', $data['id'], '物品采购');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wu_cai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WuCaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $list = WuCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('wu_cai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_wu_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ru.number|ru.title|ru.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['ru.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ru.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = WuRuModel::alias('ru')
            ->field('ru.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wu_ru', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wu_ru');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wu_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['title'])){
                return outputJson(4000, '请输入入库主题');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择入库日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入入库人');
            }



            $param['number'] = (new XingService())->create_wu_ru_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入入库明细');
            }


            Db::startTrans();
            try {
                $id = WuRuModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['ruid'] = $id;
                    WuRuListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'wu_ru',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_ru', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_ru', $id, '物品入库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new XingService())->create_wu_ru_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WuRuModel::where(['id'=>$param['id']])->find();

            if(empty($param['title'])){
                return outputJson(4000, '请输入入库主题');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择入库日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入入库人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入入库明细');
            }

            Db::startTrans();
            try {
                WuRuModel::update($param);

                WuRuListModel::where(['ruid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['ruid'] = $param['id'];
                    WuRuListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'wu_ru',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_ru', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_ru', $param['id'], '物品入库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuRuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_ru', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wu_ru(Request $request){
        $param = $request->post();
        $data = WuRuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            WuRuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wu_ru', $data['id'], '物品入库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wu_ru(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WuRuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $list = WuRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('wu_ru', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_wu_guan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|list.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['list.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['list.unit','like', '%'.$param['unit'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }


        $data['data'] = WuRuListModel::alias('list')
            ->field('list.*,sum(num) as runum')
            ->join('wu_ru ru','ru.id=list.ruid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wu_guan', 'ru'))
            ->order('list.id DESC')
            ->group('list.name,list.unit')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['comid'],'name'=>$item['name'],'unit'=>$item['unit']])->sum('num');
                $item['chunum'] += WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->sum('list.num');
                $item['kucun'] = round($item['runum']-$item['chunum'], 2);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wu_guan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_wu_guan(Request $request){
        $param = $request->post();

        $list = [];

        $ru = WuRuListModel::alias('list')
            ->field('list.*,ru.number,ru.dt,ru.baner')
            ->join('wu_ru ru','ru.id=list.ruid')
            ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
            ->where(['list.name'=>$param['name'],'list.unit'=>$param['unit']])
            ->select()->toArray();
        foreach ($ru as $item){
            $list[] = [
                'type' => 'ru',
                'id' => $item['id'],
                'number' => $item['number'],
                'dt' => $item['dt'],
                'dongname' => '入库',
                'name' => $item['name'],
                'unit' => $item['unit'],
                'num' => $item['num'],
                'baner' => $item['baner'],
            ];
        }

        $chu = WuChuModel::where(['comid'=>$this->userinfo['comid'],'name'=>$param['name'],'unit'=>$param['unit']])->select()->toArray();
        foreach ($chu as $item){
            $list[] = [
                'type' => 'chu',
                'id' => $item['id'],
                'number' => $item['number'],
                'dt' => $item['dt'],
                'dongname' => '出库',
                'name' => $item['name'],
                'unit' => $item['unit'],
                'num' => $item['num'],
                'baner' => $item['baner'],
            ];
        }


        $ling = WuLingListModel::alias('list')
            ->field('list.*,ling.number,ling.created_at,ling.flowuser')
            ->join('wu_ling ling','ling.id=list.lingid')
            ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
            ->where(['list.name'=>$param['name'],'list.unit'=>$param['unit']])
            ->select()->toArray();
        foreach ($ling as $item){
            $list[] = [
                'type' => 'ling',
                'id' => $item['id'],
                'number' => $item['number'],
                'dt' => date("Y-m-d", strtotime($item['created_at'])),
                'dongname' => '领用',
                'name' => $item['name'],
                'unit' => $item['unit'],
                'num' => $item['num'],
                'baner' => $item['flowuser'],
            ];
        }

        $dtSort = array_column($list,'dt');
        array_multisort($dtSort,SORT_ASC,$list);


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function chu_wu_guan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择出库日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入领取人');
            }



            $param['number'] = (new XingService())->create_wu_chu_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }

            Db::startTrans();
            try {
                foreach($list as $vv){
                    $vv['comid'] = $param['comid'];
                    $vv['number'] = $param['number'];
                    $vv['dt'] = $param['dt'];
                    $vv['baner'] = $param['baner'];
                    $vv['created_user'] = $param['created_user'];
                    $vv['created_at'] = $param['created_at'];
                    WuChuModel::insert($vv);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['name'] = $request->get('name','','string');
            $data['unit'] = $request->get('unit','','string');
            $data['runum'] = WuRuListModel::alias('list')
                ->join('wu_ru ru','ru.id=list.ruid')
                ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
                ->where(['list.name'=>$data['name'],'list.unit'=>$data['unit']])
                ->sum('list.num');
            $data['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$data['name'],'unit'=>$data['unit']])->sum('num');
            $data['chunum'] += WuLingListModel::alias('list')
                ->join('wu_ling ling','ling.id=list.lingid')
                ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                ->where(['list.name'=>$data['name'],'list.unit'=>$data['unit']])
                ->sum('list.num');
            $data['kucun'] = $data['runum']-$data['chunum'];


            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new XingService())->create_wu_chu_number();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function chu_edit_wu_guan(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                foreach($list as $vv){
                    WuChuModel::update($vv);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data = WuChuModel::where(['id'=>$id])->find();

            $data['runum'] = WuRuListModel::alias('list')
                ->join('wu_ru ru','ru.id=list.ruid')
                ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
                ->where(['list.name'=>$data['name'],'list.unit'=>$data['unit']])
                ->sum('list.num');
            $data['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$data['name'],'unit'=>$data['unit']])
                ->where('id', 'neq',$data['id'])->sum('num');
            $data['chunum'] += WuLingListModel::alias('list')
                ->join('wu_ling ling','ling.id=list.lingid')
                ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                ->where(['list.name'=>$data['name'],'list.unit'=>$data['unit']])
                ->sum('list.num');
            $data['kucun'] = $data['runum']-$data['chunum'];
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wu_chu(Request $request){
        $param = $request->post();
        $data = WuChuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            WuChuModel::where(['id'=>$data['id']])->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_wu_ling(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.isturn','=',1];
        $where[] = ['ling.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.flowuser|ling.flowdept|ling.number|ling.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['ling.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['ling.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ling.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['ling.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $where[] = ['ling.created_at','between', explode(' - ', $param['created_at'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ling.status','=', $param['status']];
        }

        $data['data'] = WuLingModel::alias('ling')
            ->field('ling.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wu_ling', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wu_ling');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['flowuser'])){
                return outputJson(4000, '请输入申请人');
            }
            if(empty($param['flowdept'])){
                return outputJson(4000, '请输入申购部门');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入领用主题');
            }




            $param['number'] = (new XingService())->create_wu_ling_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入领用明细');
            }


            Db::startTrans();
            try {
                $id = WuLingModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['lingid'] = $id;
                    WuLingListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'wu_ling',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_ling', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_ling', $id, '物品领用');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['number'] = (new XingService())->create_wu_ling_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WuLingModel::where(['id'=>$param['id']])->find();

            if(empty($param['flowuser'])){
                return outputJson(4000, '请输入申请人');
            }
            if(empty($param['flowdept'])){
                return outputJson(4000, '请输入申购部门');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入领用主题');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入领用明细');
            }

            Db::startTrans();
            try {
                WuLingModel::update($param);

                WuLingListModel::where(['lingid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['lingid'] = $param['id'];
                    WuLingListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'wu_ling',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_ling', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_ling', $param['id'], '物品领用');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuLingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['runum'] = WuRuListModel::alias('list')
                    ->join('wu_ru ru','ru.id=list.ruid')
                    ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->sum('list.num');
                $item['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$item['name'],'unit'=>$item['unit']])->sum('num');
                $item['chunum'] += WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->where('list.id','neq',$item['id'])
                    ->sum('list.num');
                $item['kucun'] = $item['runum']-$item['chunum'];
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_ling', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wu_ling(Request $request){
        $param = $request->post();
        $data = WuLingModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            WuLingModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wu_ling', $data['id'], '物品领用');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wu_ling(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WuLingModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $list = WuLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['runum'] = WuRuListModel::alias('list')
                ->join('wu_ru ru','ru.id=list.ruid')
                ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
                ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                ->sum('list.num');
            $item['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$item['name'],'unit'=>$item['unit']])->sum('num');
            $item['chunum'] += WuLingListModel::alias('list')
                ->join('wu_ling ling','ling.id=list.lingid')
                ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                ->where('list.id','neq',$item['id'])
                ->sum('list.num');
            $item['kucun'] = $item['runum']-$item['chunum'];
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('wu_ling', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




    public function list_wen_ban(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ban.deleted_user','=', 0];
        $where[] = ['ban.comid','=',$this->userinfo['comid']];
        $where[] = ['ban.isturn','=',1];
        $where[] = ['ban.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ban.number|ban.title|ban.zhuti','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['wentype']) && !empty($param['wentype'])){
            $where[] = ['ban.wentype','=', $param['wentype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ban.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['wenzi']) && !empty($param['wenzi'])){
            $where[] = ['ban.wenzi','=', $param['wenzi']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ban.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['wenlevel']) && !empty($param['wenlevel'])){
            $where[] = ['ban.wenlevel','=', $param['wenlevel']];
        }
        if(isset($param['wenjin']) && !empty($param['wenjin'])){
            $where[] = ['ban.wenjin','=', $param['wenjin']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['ban.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['zhuti']) && !empty($param['zhuti'])){
            $where[] = ['ban.zhuti','like', '%'.$param['zhuti'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ban.status','=', $param['status']];
        }

        $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
        $data['is_wentype'] = isset($param['wentype']) ? $param['wentype'] : '';

        $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
        $data['is_wenzi'] = isset($param['wenzi']) ? $param['wenzi'] : '';

        $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
        $data['is_wenlevel'] = isset($param['wenlevel']) ? $param['wenlevel'] : '';

        $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
        $data['is_wenjin'] = isset($param['wenjin']) ? $param['wenjin'] : '';

        $data['data'] = WenBanModel::alias('ban')
            ->field('ban.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wen_ban', 'ban'))
            ->order('ban.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $item['wentype']);
                $item['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $item['wenzi']);
                $item['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $item['wenlevel']);
                $item['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $item['wenjin']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wen_ban');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wen_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['wentype'])){
                return outputJson(4000, '请选择公文类型');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择拟稿日期');
            }
            if(empty($param['wenzi'])){
                return outputJson(4000, '请选择发文字号');
            }
            if(empty($param['number'])){
                return outputJson(4000, '请输入公文编号');
            }
            if(empty($param['wenlevel'])){
                return outputJson(4000, '请选择公文秘级');
            }
            if(empty($param['wenjin'])){
                return outputJson(4000, '请选择紧急程度');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['fadept'])){
                return outputJson(4000, '请输入发文单位');
            }
            if(empty($param['zhudept'])){
                return outputJson(4000, '请输入主送单位');
            }




            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = WenBanModel::insertGetId($param);
                (new OptService())->relation_file($files,'wen_ban',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wen_ban', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wen_ban', $id, '拟办发文');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['year'] = date("Y");
            $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
            $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
            $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
            $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wen_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WenBanModel::where(['id'=>$param['id']])->find();

            if(empty($param['wentype'])){
                return outputJson(4000, '请选择公文类型');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择拟稿日期');
            }
            if(empty($param['wenzi'])){
                return outputJson(4000, '请选择发文字号');
            }
            if(empty($param['number'])){
                return outputJson(4000, '请输入公文编号');
            }
            if(empty($param['wenlevel'])){
                return outputJson(4000, '请选择公文秘级');
            }
            if(empty($param['wenjin'])){
                return outputJson(4000, '请选择紧急程度');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['fadept'])){
                return outputJson(4000, '请输入发文单位');
            }
            if(empty($param['zhudept'])){
                return outputJson(4000, '请输入主送单位');
            }



            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                WenBanModel::update($param);
                (new OptService())->relation_file($files,'wen_ban',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wen_ban', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wen_ban', $param['id'], '拟办发文');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WenBanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['year'] = date("Y");
            $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
            $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
            $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
            $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
            $data['files'] = (new OptService())->get_file('wen_ban', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wen_ban(Request $request){
        $param = $request->post();
        $data = WenBanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            WenBanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wen_ban', $data['id'], '拟办发文');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wen_ban(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WenBanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $data['data']['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $data['data']['wentype']);
        $data['data']['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $data['data']['wenzi']);
        $data['data']['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $data['data']['wenlevel']);
        $data['data']['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $data['data']['wenjin']);


        $data['files'] = (new OptService())->get_file('wen_ban', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_wen_fa(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fa.deleted_user','=', 0];
        $where[] = ['fa.comid','=',$this->userinfo['comid']];
        $where[] = ['fa.isturn','=',1];
        $where[] = ['fa.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ban.number|ban.title|ban.zhuti','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['wentype']) && !empty($param['wentype'])){
            $where[] = ['ban.wentype','=', $param['wentype']];
        }
        if(isset($param['wenzi']) && !empty($param['wenzi'])){
            $where[] = ['ban.wenzi','=', $param['wenzi']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ban.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['wenlevel']) && !empty($param['wenlevel'])){
            $where[] = ['ban.wenlevel','=', $param['wenlevel']];
        }
        if(isset($param['wenjin']) && !empty($param['wenjin'])){
            $where[] = ['ban.wenjin','=', $param['wenjin']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['ban.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['zhuti']) && !empty($param['zhuti'])){
            $where[] = ['ban.zhuti','like', '%'.$param['zhuti'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['fa.dt','between', explode(' - ', $param['dt'])];
        }


        $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
        $data['is_wentype'] = isset($param['wentype']) ? $param['wentype'] : '';

        $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
        $data['is_wenzi'] = isset($param['wenzi']) ? $param['wenzi'] : '';

        $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
        $data['is_wenlevel'] = isset($param['wenlevel']) ? $param['wenlevel'] : '';

        $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
        $data['is_wenjin'] = isset($param['wenjin']) ? $param['wenjin'] : '';

        $data['data'] = WenFaModel::alias('fa')
            ->field('fa.*,ban.wentype,ban.wenzi,ban.number,ban.wenlevel,ban.wenjin,ban.title,ban.zhuti,ban.deptid as bandeptid,ban.zhudept')
            ->join('wen_ban ban','ban.id=fa.banid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wen_fa', 'fa'))
            ->order('fa.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $item['wentype']);
                $item['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $item['wenzi']);
                $item['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $item['wenlevel']);
                $item['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $item['wenjin']);
                $item['bandeptid_name'] = DeptModel::get_name_by_ids($item['bandeptid']);
                $item['zhudept_name'] = DeptModel::get_name_by_ids($item['zhudept']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wen_fa');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wen_fa(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['banid'])){
                return outputJson(4000, '请选择拟办发文');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择发文日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = WenFaModel::insertGetId($param);
                (new OptService())->relation_file($files,'wen_fa',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wen_fa', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wen_fa', $id, '发文');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wen_fa(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WenFaModel::where(['id'=>$param['id']])->find();

            if(empty($param['banid'])){
                return outputJson(4000, '请选择拟办发文');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择发文日期');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                WenFaModel::update($param);
                (new OptService())->relation_file($files,'wen_fa',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wen_fa', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wen_fa', $param['id'], '发文');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WenFaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $ban = WenBanModel::where(['id'=>$data['data']['banid']])->find();
            $ban['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $ban['wentype']);
            $ban['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $ban['wenzi']);
            $ban['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $ban['wenlevel']);
            $ban['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $ban['wenjin']);
            $ban['deptid_name'] = DeptModel::get_name_by_pk($ban['deptid']);
            $ban['zhudept_name'] = DeptModel::get_name_by_ids($ban['zhudept']);
            $ban['chaodept_name'] = DeptModel::get_name_by_ids($ban['chaodept']);
            $data['ban'] = $ban;

            $data['files'] = (new OptService())->get_file('wen_fa', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wen_fa(Request $request){
        $param = $request->post();
        $data = WenFaModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            WenFaModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wen_fa', $data['id'], '发文');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wen_fa(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WenFaModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $ban = WenBanModel::where(['id'=>$data['data']['banid']])->find();
        $ban['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $ban['wentype']);
        $ban['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $ban['wenzi']);
        $ban['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $ban['wenlevel']);
        $ban['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $ban['wenjin']);
        $ban['deptid_name'] = DeptModel::get_name_by_pk($ban['deptid']);
        $ban['zhudept_name'] = DeptModel::get_name_by_ids($ban['zhudept']);
        $ban['chaodept_name'] = DeptModel::get_name_by_ids($ban['chaodept']);
        $data['ban'] = $ban;


        $data['files'] = (new OptService())->get_file('wen_fa', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_wen_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shou.number|shou.title|shou.zhuti','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['wenlevel']) && !empty($param['wenlevel'])){
            $where[] = ['shou.wenlevel','=', $param['wenlevel']];
        }
        if(isset($param['wenjin']) && !empty($param['wenjin'])){
            $where[] = ['shou.wenjin','=', $param['wenjin']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['zhuti']) && !empty($param['zhuti'])){
            $where[] = ['shou.zhuti','like', '%'.$param['zhuti'].'%'];
        }

        $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
        $data['is_wenlevel'] = isset($param['wenlevel']) ? $param['wenlevel'] : '';

        $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
        $data['is_wenjin'] = isset($param['wenjin']) ? $param['wenjin'] : '';

        $data['data'] = WenShouModel::alias('shou')
            ->field('shou.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wen_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $item['wenlevel']);
                $item['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $item['wenjin']);
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wen_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wen_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['dt'])){
                return outputJson(4000, '请选择来文日期');
            }
            if(empty($param['number'])){
                return outputJson(4000, '请输入公文编号');
            }
            if(empty($param['wenlevel'])){
                return outputJson(4000, '请选择公文秘级');
            }
            if(empty($param['wenjin'])){
                return outputJson(4000, '请选择紧急程度');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['laidept'])){
                return outputJson(4000, '请输入来文单位');
            }
            if(empty($param['deptid'])){
                return outputJson(4000, '请选择接收单位');
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = WenShouModel::insertGetId($param);
                (new OptService())->relation_file($files,'wen_shou',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wen_shou', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wen_shou', $id, '收文');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
            $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wen_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WenShouModel::where(['id'=>$param['id']])->find();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择来文日期');
            }
            if(empty($param['number'])){
                return outputJson(4000, '请输入公文编号');
            }
            if(empty($param['wenlevel'])){
                return outputJson(4000, '请选择公文秘级');
            }
            if(empty($param['wenjin'])){
                return outputJson(4000, '请选择紧急程度');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['laidept'])){
                return outputJson(4000, '请输入来文单位');
            }
            if(empty($param['deptid'])){
                return outputJson(4000, '请选择接收单位');
            }



            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                WenShouModel::update($param);
                (new OptService())->relation_file($files,'wen_shou',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wen_shou', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wen_shou', $param['id'], '收文');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WenShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['deptid'], 'dept');


            $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
            $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
            $data['files'] = (new OptService())->get_file('wen_shou', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wen_shou(Request $request){
        $param = $request->post();
        $data = WenShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            WenShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wen_shou', $data['id'], '收文');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wen_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WenShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }



        $data['data']['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $data['data']['wenlevel']);
        $data['data']['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $data['data']['wenjin']);
        $data['data']['deptid_name'] = DeptModel::get_name_by_pk($data['data']['deptid']);


        $data['files'] = (new OptService())->get_file('wen_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_wen_fen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fen.deleted_user','=', 0];
        $where[] = ['fen.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['fen.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['fen.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['fen.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['fentype']) && !empty($param['fentype'])){
            $where[] = ['fen.fentype','=', $param['fentype']];
        }
        if(isset($param['zhongdt']) && !empty($param['zhongdt'])){
            $where[] = ['fen.zhongdt','between', explode(' - ', $param['zhongdt'])];
        }

        $data['fentype'] = (new XingService())->get_wen_fentype();
        $data['is_fentype'] = isset($param['fentype']) ? $param['fentype'] : '';

        $data['data'] = WenFenModel::alias('fen')
            ->field('fen.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wen_fen', 'fen'))
            ->order('fen.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['fentype'] == 1){
                    $banid = WenFaModel::where(['id'=>$item['faid']])->value('banid');
                    $item['wenname'] = WenBanModel::where(['id'=>$banid])->value('title');
                }else{
                    $item['wenname'] = WenShouModel::where(['id'=>$item['shouid']])->value('title');
                }

                $item['fentype'] = (new XingService())->get_wen_fentype_show($item['fentype']);
                $item['fendui_name'] = $item['fendui'] == 'dept' ? '部门' : '用户';
                $item['fenuser'] = UserModel::get_realname_by_ids($item['fenuser']);
                $item['reader_name'] = UserModel::get_realname_by_ids($item['reader']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('wen_fen');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_wenmy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fen.deleted_user','=', 0];
        $where[] = ['fen.comid','=',$this->userinfo['comid']];
        $where[] = ['fen.zhongdt','>=', date("Y-m-d")];

        $wherefenuser = "find_in_set(".$this->userinfo['id'].",fenuser)";

        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['fen.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['fen.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title|ban.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['fentype']) && !empty($param['fentype'])){
            $where[] = ['fen.fentype','=', $param['fentype']];
        }
        if(isset($param['zhongdt']) && !empty($param['zhongdt'])){
            $where[] = ['fen.zhongdt','between', explode(' - ', $param['zhongdt'])];
        }



        $data['fentype'] = (new XingService())->get_wen_fentype();
        $data['is_fentype'] = isset($param['fentype']) ? $param['fentype'] : '';


        $data['data'] = WenFenModel::alias('fen')
            ->field('fen.*,shou.title as wenname,ban.title as banname')
            ->leftJoin('wen_shou shou','shou.id=fen.shouid')
            ->leftJoin('wen_fa fa','fa.id=fen.faid')
            ->leftJoin('wen_ban ban','ban.id=fa.banid')
            ->where($where)
            ->where($wherefenuser)
            ->order('fen.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['fentype'] == 1){
                    $item['wenname'] = $item['banname'];
                }

                $item['fentype'] = (new XingService())->get_wen_fentype_show($item['fentype']);
                $item['fendui_name'] = $item['fendui'] == 'dept' ? '部门' : '用户';
                $item['fenuser'] = UserModel::get_realname_by_ids($item['fenuser']);

                $item['read'] = '未阅';
                if(in_array($this->userinfo['id'], explode(',', $item['reader']))){
                    $item['read'] = '已阅';
                }

                return $item;
            })->toArray();


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_wen_fen(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择分发日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入分发人');
            }

            if($param['fentype'] == 1){
                if(empty($param['faid'])){
                    return outputJson(4000, '请选择公文');
                }
                $param['shouid'] = 0;
            }else{
                if(empty($param['shouid'])){
                    return outputJson(4000, '请选择公文');
                }
                $param['faid'] = 0;
            }


            if(empty($param['fener'])){
                return outputJson(4000, '请选择分发对象');
            }




            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['fenuser'] = (new XingService())->get_wenfen_fenuser_by_fendui($param['fendui'], $param['fener']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = WenFenModel::insertGetId($param);


                if($param['fentype'] == 1){
                    $banid = WenFaModel::where(['id'=>$param['faid']])->value('banid');
                    $title = WenBanModel::where(['id'=>$banid])->value('title');
                }else{
                    $title = WenShouModel::where(['id'=>$param['shouid']])->value('title');
                }
                $mess = "[".$this->userinfo['realname']."]分发了公文[".$title."]，请查看！";
                (new OptService())->add_todo($param['fenuser'], '公文分发',$mess, $id, 'wen_fen');


                (new OptService())->relation_file($files,'wen_fen',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wen_fen', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wen_fen', $id, '公文分发');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['fentype'] = (new XingService())->get_wen_fentype();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wen_fen(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = WenFenModel::where(['id'=>$param['id']])->find();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择分发日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入分发人');
            }

            if($param['fentype'] == 1){
                if(empty($param['faid'])){
                    return outputJson(4000, '请选择公文');
                }
                $param['shouid'] = 0;
            }else{
                if(empty($param['shouid'])){
                    return outputJson(4000, '请选择公文');
                }
                $param['faid'] = 0;
            }


            if(empty($param['fener'])){
                return outputJson(4000, '请选择分发对象');
            }



            $param['fenuser'] = (new XingService())->get_wenfen_fenuser_by_fendui($param['fendui'], $param['fener']);
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                WenFenModel::update($param);
                (new OptService())->relation_file($files,'wen_fen',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wen_fen', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wen_fen', $param['id'], '公文分发');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WenFenModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $weninfo = [];
            if($data['data']['fentype'] == 1){
                $weninfo = WenFaModel::get_info_by_pk($data['data']['faid']);
                $weninfo['title'] = WenBanModel::where(['id'=>$weninfo['banid']])->value('title');
            }else{
                $weninfo = WenShouModel::get_info_by_pk($data['data']['shouid']);
            }
            $data['weninfo'] = $weninfo;

            if($data['data']['fendui'] == 'dept'){
                $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['fener'], 'dept');
            }
            if($data['data']['fendui'] == 'user'){
                $data['data']['userid_info'] = (new UserService())->get_users_part_arr($data['data']['fener'], 'user');
            }

            $data['fentype'] = (new XingService())->get_wen_fentype();
            $data['files'] = (new OptService())->get_file('wen_fen', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_wen_fen(Request $request){
        $param = $request->post();
        $data = WenFenModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            WenFenModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'wen_fen', $data['id'], '公文分发');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_wen_fen(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = WenFenModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $readerArr = explode(',', $data['data']['reader'].','.$this->userinfo['id']);
        $reader = implode(',', array_unique($readerArr));
        WenFenModel::where(['id'=>$data['data']['id']])->update(['reader'=>$reader]);
        $data['data'] = WenFenModel::get_info_by_pk($id);


        $weninfo = [];
        if($data['data']['fentype'] == 1){
            $weninfo = WenFaModel::get_info_by_pk($data['data']['faid']);
            $weninfo['title'] = WenBanModel::where(['id'=>$weninfo['banid']])->value('title');
        }else{
            $weninfo = WenShouModel::get_info_by_pk($data['data']['shouid']);
        }
        $data['weninfo'] = $weninfo;

        if($data['data']['fendui'] == 'dept'){
            $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['fener'], 'dept');
        }
        if($data['data']['fendui'] == 'user'){
            $data['data']['userid_info'] = (new UserService())->get_users_part_arr($data['data']['fener'], 'user');
        }

        $data['data']['fentype_name'] = (new XingService())->get_wen_fentype_show($data['data']['fentype']);
        $data['data']['fendui_name'] = $data['data']['fendui'] == 'dept' ? '部门' : '用户';
        $data['data']['fenuser'] = UserModel::get_realname_by_ids($data['data']['fenuser']);

        $data['files'] = (new OptService())->get_file('wen_fen', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_meet(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['meet.deleted_user','=', 0];
        $where[] = ['meet.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['meet.title|meet.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['room']) && !empty($param['room'])){
            $where[] = ['meet.room','=', $param['room']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['meet.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['meet.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['meet.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['meet.enddt','between', explode(' - ', $param['enddt'])];
        }


        $data['room'] = (new OptService())->get_base_info('qi_meet_room');
        $data['is_room'] = isset($param['room']) ? $param['room'] : '';


        $data['data'] = MeetModel::alias('meet')
            ->field('meet.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('meet', 'meet'))
            ->order('meet.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['room'] = (new OptService())->get_base_showname('qi_meet_room', $item['room']);
                $item['caner_name'] = UserModel::get_realname_by_ids($item['caner']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('meet');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_meetmy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['meet.deleted_user','=', 0];
        $where[] = ['meet.comid','=',$this->userinfo['comid']];

        $wherecaner = "find_in_set(".$this->userinfo['id'].",caner)";

        if(isset($param['room']) && !empty($param['room'])){
            $where[] = ['meet.room','=', $param['room']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['meet.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['meet.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['meet.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['meet.enddt','between', explode(' - ', $param['enddt'])];
        }


        $data['room'] = (new OptService())->get_base_info('qi_meet_room');
        $data['is_room'] = isset($param['room']) ? $param['room'] : '';


        $data['data'] = MeetModel::alias('meet')
            ->field('meet.*')
            ->where($where)
            ->where($wherecaner)
            ->order('meet.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['room'] = (new OptService())->get_base_showname('qi_meet_room', $item['room']);
                $item['caner_name'] = UserModel::get_realname_by_ids($item['caner']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_meetfa(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['meet.deleted_user','=', 0];
        $where[] = ['meet.comid','=',$this->userinfo['comid']];
        $where[] = ['meet.created_user','=',$this->userinfo['id']];

        if(isset($param['room']) && !empty($param['room'])){
            $where[] = ['meet.room','=', $param['room']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['meet.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['meet.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['meet.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['meet.enddt','between', explode(' - ', $param['enddt'])];
        }


        $data['room'] = (new OptService())->get_base_info('qi_meet_room');
        $data['is_room'] = isset($param['room']) ? $param['room'] : '';


        $data['data'] = MeetModel::alias('meet')
            ->field('meet.*')
            ->where($where)
            ->order('meet.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['room'] = (new OptService())->get_base_showname('qi_meet_room', $item['room']);
                $item['caner_name'] = UserModel::get_realname_by_ids($item['caner']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_meet(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['room'])){
                return outputJson(4000, '请选择会议室');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入会议主题');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入发起人');
            }
            if(empty($param['startdt'])){
                return outputJson(4000, '请选择开始时间');
            }
            if(empty($param['enddt'])){
                return outputJson(4000, '请选择预计结束时间');
            }
            if(empty($param['caner'])){
                return outputJson(4000, '请选择参会人');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = MeetModel::insertGetId($param);


                $mess = "[".$this->userinfo['realname']."]发起了[".$param['title']."]，请查看并参会！";
                (new OptService())->add_todo($param['caner'], '会议',$mess, $id, 'meet');


                (new OptService())->relation_file($files,'meet',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'meet', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'meet', $id, '会议');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['baner'] = $this->userinfo['realname'];
            $data['room'] = (new OptService())->get_base_info('qi_meet_room');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_meet(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = MeetModel::where(['id'=>$param['id']])->find();

            if(empty($param['room'])){
                return outputJson(4000, '请选择会议室');
            }
            if(empty($param['title'])){
                return outputJson(4000, '请输入会议主题');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入发起人');
            }
            if(empty($param['startdt'])){
                return outputJson(4000, '请选择开始时间');
            }
            if(empty($param['enddt'])){
                return outputJson(4000, '请选择预计结束时间');
            }
            if(empty($param['caner'])){
                return outputJson(4000, '请选择参会人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                MeetModel::update($param);
                (new OptService())->relation_file($files,'meet',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'meet', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'meet', $param['id'], '会议');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = MeetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['caner_info'] = (new UserService())->get_users_part_arr($data['data']['caner'], 'user');

            $data['room'] = (new OptService())->get_base_info('qi_meet_room');
            $data['files'] = (new OptService())->get_file('meet', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_meet(Request $request){
        $param = $request->post();
        $data = MeetModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            MeetModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'meet', $data['id'], '会议');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_meet(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = MeetModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['room'] = (new OptService())->get_base_showname('qi_meet_room', $data['data']['room']);
        $data['data']['caner_name'] = UserModel::get_realname_by_ids($data['data']['caner']);


        $data['files'] = (new OptService())->get_file('meet', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_seal(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['seal.deleted_user','=', 0];
        $where[] = ['seal.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['seal.name|bao.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sealtype']) && !empty($param['sealtype'])){
            $where[] = ['seal.sealtype','=', $param['sealtype']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['seal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['seal.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baoname']) && !empty($param['baoname'])){
            $where[] = ['bao.realname','like', '%'.$param['baoname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['seal.chanid','=', $param['chanid']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['sealtype'] = (new OptService())->get_base_info('qi_seal_sealtype');
        $data['is_sealtype'] = isset($param['sealtype']) ? $param['sealtype'] : '';


        $data['data'] = SealModel::alias('seal')
            ->field('seal.*,bao.realname as baoname')
            ->leftJoin('user bao','bao.id=seal.baoid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('seal', 'seal'))
            ->order('seal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['sealtype'] = (new OptService())->get_base_showname('qi_seal_sealtype', $item['sealtype']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('seal');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_seal(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }
            if(empty($param['sealtype'])){
                return outputJson(4000, '请选择印章类型');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入印章名称');
            }
            if((new XingService())->check_seal_name($param['name'])){
                return outputJson(4000, '印章名称已存在');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = SealModel::insertGetId($param);
                (new OptService())->relation_file($files,'seal',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal', $id, '印章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['baoid_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            $data['sealtype'] = (new OptService())->get_base_info('qi_seal_sealtype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SealModel::where(['id'=>$param['id']])->find();

            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }
            if(empty($param['sealtype'])){
                return outputJson(4000, '请选择印章类型');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入印章名称');
            }
            if((new XingService())->check_seal_name($param['name'], $param['id'])){
                return outputJson(4000, '印章名称已存在');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                SealModel::update($param);
                (new OptService())->relation_file($files,'seal',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'seal', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'seal', $param['id'], '印章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['data']['baoid_info'] = (new UserService())->get_users_part_arr($data['data']['baoid'], 'user');
            $data['sealtype'] = (new OptService())->get_base_info('qi_seal_sealtype');
            $data['files'] = (new OptService())->get_file('seal', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_seal(Request $request){
        $param = $request->post();
        $data = SealModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            SealModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'seal', $data['id'], '印章');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_seal(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SealModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['sealtype'] = (new OptService())->get_base_showname('qi_seal_sealtype', $data['data']['sealtype']);

        $data['data']['chanid_name'] = DeptModel::get_name_by_pk($data['data']['chanid']);
        $data['data']['baoid_name'] = UserModel::get_realname_by_pk($data['data']['baoid']);


        $data['files'] = (new OptService())->get_file('seal', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_seal_gai(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['gai.deleted_user','=', 0];
        $where[] = ['gai.comid','=',$this->userinfo['comid']];
        $where[] = ['gai.isturn','=',1];
        $where[] = ['gai.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['gai.flowuser|gai.flowdept','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['gai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['gai.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $where[] = ['gai.created_at','between', explode(' - ', $param['created_at'])];
        }
        if(isset($param['isgai']) && $param['isgai'] != ''){
            $where[] = ['gai.isgai','=', $param['isgai']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['gai.status','=', $param['status']];
        }


        $data['data'] = SealGaiModel::alias('gai')
            ->field('gai.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('seal_gai', 'gai'))
            ->order('gai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['isgai_name'] = $item['isgai'] > 0 ? '已盖章' : '未盖章';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('seal_gai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['useer'])){
                return outputJson(4000, '请输入用章人');
            }
            if(empty($param['depter'])){
                return outputJson(4000, '请输入用章部门');
            }

            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['status'] = 1;
            $newparam['isturn'] = 1;
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();


            $list = array_filter($param['list']);
            unset($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            $newparam['useer'] = $param['useer'];
            $newparam['depter'] = $param['depter'];


            Db::startTrans();
            try {
                $id = SealGaiModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['gaiid'] = $id;
                    SealGaiListModel::insert($vv);
                }

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_gai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_gai', $id, '日常盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['useer'] = $this->userinfo['realname'];
            $data['depter'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SealGaiModel::where(['id'=>$param['id']])->find();

            if(empty($param['useer'])){
                return outputJson(4000, '请输入用章人');
            }
            if(empty($param['depter'])){
                return outputJson(4000, '请输入用章部门');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }


            $newparam['id'] = $param['id'];
            $newparam['useer'] = $param['useer'];
            $newparam['depter'] = $param['depter'];

            Db::startTrans();
            try {
                SealGaiModel::update($newparam);

                SealGaiListModel::where(['gaiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gaiid'] = $param['id'];
                    SealGaiListModel::insert($vv);
                }

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'seal_gai', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'seal_gai', $param['id'], '日常盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealGaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }



            $list = SealGaiListModel::where(['gaiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_seal_gai(Request $request){
        $param = $request->post();
        $data = SealGaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            SealGaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'seal_gai', $data['id'], '日常盖章');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_seal_gai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SealGaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $list = SealGaiListModel::where(['gaiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['cusname'] = count($item['filearr']);
        }
        $data['list'] = $list;

        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gai_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SealGaiModel::where(['id'=>$param['id']])->find();


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            Db::startTrans();
            try {
                SealGaiModel::where(['id'=>$param['id']])->update(['isgai'=>1]);
                foreach($list as $vv){
                    SealGaiListModel::update($vv);
                }

                (new OptService())->add_flow_log('盖章', 'seal_gai', $param['id']);
                (new OptService())->add_log_list('盖章', 'seal_gai', $param['id'], '日常盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealGaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = SealGaiListModel::where(['gaiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($data['data']['isgai'] < 1){
                    $item['gaidt'] = date("Y-m-d");
                    $item['gaier'] = $this->userinfo['realname'];
                }
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function list_seal_gai_detail(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['gai.deleted_user','=', 0];
        $where[] = ['gai.comid','=',$this->userinfo['comid']];
        $where[] = ['gai.isturn','=',1];
        $where[] = ['gai.isgai','=',1];
        $where[] = ['gai.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['gai.flowuser|gai.flowdept|list.name|gaier|list.jieer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['useer']) && !empty($param['useer'])){
            $where[] = ['gai.useer','like', '%'.$param['useer'].'%'];
        }
        if(isset($param['depter']) && !empty($param['depter'])){
            $where[] = ['gai.depter','like', '%'.$param['depter'].'%'];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['list.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['yongtype']) && !empty($param['yongtype'])){
            $where[] = ['list.yongtype','like', '%'.$param['yongtype'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['list.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['isfan']) && !empty($param['isfan'])){
            $where[] = ['list.isfan','=', $param['isfan']];
        }
        if(isset($param['gaidt']) && !empty($param['gaidt'])){
            $where[] = ['list.gaidt','between', explode(' - ', $param['gaidt'])];
        }
        if(isset($param['gaier']) && !empty($param['gaier'])){
            $where[] = ['list.gaier','=', $param['gaier']];
        }
        if(isset($param['fandt']) && !empty($param['fandt'])){
            $where[] = ['list.fandt','between', explode(' - ', $param['fandt'])];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['list.jieer','like', '%'.$param['jieer'].'%'];
        }


        $data['data'] = SealGaiListModel::alias('list')
            ->field('list.*,gai.flowuser,gai.flowdept')
            ->join('seal_gai gai','gai.id=list.gaiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('seal_gai_detail', 'gai'))
            ->order('gai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('seal_gai_detail');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function fan_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            Db::startTrans();
            try {
                foreach($list as $vv){
                    SealGaiListModel::update($vv);
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $ids = $request->get('ids',0,'string');

            $list = SealGaiListModel::where('id','in',$ids)->where(['isfan'=>'是'])->select()->toArray();
            foreach ($list as &$item){
                $gai = SealGaiModel::where(['id'=>$item['gaiid']])->find();
                $item['flowuser'] = $gai['flowuser'];
                if(empty($item['fandt'])){
                    $item['fandt'] = date("Y-m-d");
                }
                if(empty($item['jieer'])){
                    $item['jieer'] = $this->userinfo['realname'];
                }
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function list_asset(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['asset.deleted_user','=', 0];
        $where[] = ['asset.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['asset.number|asset.name|asset.gui|asset.pin|asset.danwei|asset.money|asset.cunaddr|asset.linger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['assettype']) && !empty($param['assettype'])){
            $where[] = ['asset.assettype','=', $param['assettype']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['asset.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['asset.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['gui']) && !empty($param['gui'])){
            $where[] = ['asset.gui','like', '%'.$param['gui'].'%'];
        }
        if(isset($param['pin']) && !empty($param['pin'])){
            $where[] = ['asset.pin','like', '%'.$param['pin'].'%'];
        }
        if(isset($param['danwei']) && !empty($param['danwei'])){
            $where[] = ['asset.danwei','like', '%'.$param['danwei'].'%'];
        }
        if(isset($param['caidt']) && !empty($param['caidt'])){
            $where[] = ['asset.caidt','between', explode(' - ', $param['caidt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['asset.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['cunaddr']) && !empty($param['cunaddr'])){
            $where[] = ['asset.cunaddr','like', '%'.$param['cunaddr'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['asset.state','=', $param['state']];
        }
        if(isset($param['linger']) && !empty($param['linger'])){
            $where[] = ['asset.linger','like', '%'.$param['linger'].'%'];
        }


        $data['assettype'] = (new OptService())->get_base_info('qi_asset_assettype');
        $data['is_assettype'] = isset($param['assettype']) ? $param['assettype'] : '';

        $data['state'] = (new XingService())->get_asset_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = AssetModel::alias('asset')
            ->field('asset.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('asset', 'asset'))
            ->order('asset.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['assettype'] = (new OptService())->get_base_showname('qi_asset_assettype', $item['assettype']);
                $item['state_name'] = (new XingService())->get_asset_state_show($item['state']);

                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('asset');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入信息');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            Db::startTrans();
            try {
                foreach ($list as $item){
                    $item['comid'] = $param['comid'];
                    $item['created_user'] = $param['created_user'];
                    $item['updated_user'] = $param['updated_user'];
                    $item['created_at'] = $param['created_at'];
                    $item['updated_at'] = $param['updated_at'];
                    $item['chanid'] = (new UserService())->get_dept_chanid($item['deptid']);


                    $f_arr = array_filter(explode(',', $item['files']));
                    $files = [];
                    foreach ($f_arr as $vv){
                        $files[] = ['id'=>$vv, 'fileplain'=>''];
                    }
                    unset($item['files']);

                    $id = AssetModel::insertGetId($item);
                    (new OptService())->relation_file($files,'asset',$id);
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'asset', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'asset', $id, '固定资产');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['assettype'] = (new OptService())->get_base_info('qi_asset_assettype');
            $data['state'] = (new XingService())->get_asset_state();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function addmob_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['assettype'])){
                return outputJson(4000, '请选择资产类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入资产名称');
            }
            if(empty($param['danwei'])){
                return outputJson(4000, '请输入单位');
            }
            if(empty($param['deptid'])){
                return outputJson(4000, '请选择管理部门');
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $item['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = AssetModel::insertGetId($param);

                (new OptService())->relation_file($files,'asset',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'asset', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'asset', $id, '固定资产');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['assettype'] = (new OptService())->get_base_info('qi_asset_assettype');
            $data['state'] = (new XingService())->get_asset_state();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AssetModel::where(['id'=>$param['id']])->find();

            if(empty($param['assettype'])){
                return outputJson(4000, '请选择资产类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入资产名称');
            }
            if(empty($param['danwei'])){
                return outputJson(4000, '请输入单位');
            }
            if(empty($param['deptid'])){
                return outputJson(4000, '请选择管理部门');
            }


            $param['chanid'] = (new UserService())->get_dept_chanid($param['deptid']);
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                AssetModel::update($param);
                (new OptService())->relation_file($files,'asset',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'asset', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'asset', $param['id'], '固定资产');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AssetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['deptid'], 'dept');
            $data['assettype'] = (new OptService())->get_base_info('qi_asset_assettype');
            $data['state'] = (new XingService())->get_asset_state();
            $data['files'] = (new OptService())->get_file('asset', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_asset(Request $request){
        $param = $request->post();
        $data = AssetModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AssetModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'asset', $data['id'], '固定资产');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_asset(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AssetModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['assettype'] = (new OptService())->get_base_showname('qi_asset_assettype', $data['data']['assettype']);
        $data['data']['state_name'] = (new XingService())->get_asset_state_show($data['data']['state']);

        $data['data']['deptid_name'] = DeptModel::get_name_by_pk($data['data']['deptid']);


        $data['files'] = (new OptService())->get_file('asset', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function ling_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['linger'])){
                return outputJson(4000, '请输入领用人');
            }

            Db::startTrans();
            try {
                AssetModel::where(['id'=>$param['id']])->update(['linger'=>$param['linger'],'state'=>2]);
                (new OptService())->add_flow_log('领用', 'asset', $param['id'], '', '['.$param['linger'].']'.$param['explain']);
                (new OptService())->add_log_list('领用', 'asset', $param['id'], '固定资产');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AssetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function gui_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                AssetModel::where(['id'=>$param['id']])->update(['state'=>1]);
                (new OptService())->add_flow_log('归还', 'asset', $param['id'], '', $param['explain']);
                (new OptService())->add_log_list('归还', 'asset', $param['id'], '固定资产');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AssetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function xiu_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                AssetModel::where(['id'=>$param['id']])->update(['state'=>3]);
                (new OptService())->add_flow_log('维修', 'asset', $param['id'], '', $param['explain']);
                (new OptService())->add_log_list('维修', 'asset', $param['id'], '固定资产');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AssetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fei_asset(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                AssetModel::where(['id'=>$param['id']])->update(['state'=>4]);
                (new OptService())->add_flow_log('报废', 'asset', $param['id'], '', $param['explain']);
                (new OptService())->add_log_list('报废', 'asset', $param['id'], '固定资产');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AssetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function list_news(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['news.deleted_user','=', 0];
        $where[] = ['news.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['news.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['news.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['newstype']) && !empty($param['newstype'])){
            $where[] = ['news.newstype','=', $param['newstype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['news.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['news.faer','like', '%'.$param['faer'].'%'];
        }

        $data['newstype'] = (new OptService())->get_base_info('qi_news_newstype');
        $data['is_newstype'] = isset($param['newstype']) ? $param['newstype'] : '';

        $data['data'] = NewsModel::alias('news')
            ->field('news.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('news', 'news'))
            ->order('news.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['newstype'] = (new OptService())->get_base_showname('qi_news_newstype', $item['newstype']);
                $item['created_ymd'] = date("Y-m-d", strtotime($item['created_at']));
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('news');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_news(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['newstype'])){
                return outputJson(4000, '请选择资讯分类');
            }
            if(empty($param['img']) || empty($param['img_id'])){
                return outputJson(4000, '请上传封面图片');
            }



            $param['dt'] = date("y-m-d");
            $param['faer'] = $this->userinfo['realname'];
            $param['fadept'] = DeptModel::get_name_by_pk($this->userinfo['id']);
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            unset($param['token']);


            Db::startTrans();
            try {
                $id = NewsModel::insertGetId($param);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'news', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'news', $id, '公司资讯');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("y-m-d");
            $data['faer'] = $this->userinfo['realname'];
            $data['fadept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['newstype'] = (new OptService())->get_base_info('qi_news_newstype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_news(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = NewsModel::where(['id'=>$param['id']])->find();

            if(empty($param['title'])){
                return outputJson(4000, '请输入标题');
            }
            if(empty($param['newstype'])){
                return outputJson(4000, '请选择资讯分类');
            }
            if(empty($param['img'])){
                return outputJson(4000, '请上传封面图片');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);



            Db::startTrans();
            try {
                NewsModel::update($param);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'news', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'news', $param['id'], '公司资讯');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = NewsModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['newstype'] = (new OptService())->get_base_info('qi_news_newstype');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_news(Request $request){
        $param = $request->post();
        $data = NewsModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            NewsModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'news', $data['id'], '公司资讯');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_news(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = NewsModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['deptname'] = DeptModel::get_name_by_pk($data['data']['deptid']);
        $data['data']['newstype'] = (new OptService())->get_base_showname('qi_news_newstype', $data['data']['newstype']);


        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_dangtype(Request $request){
        $param = $request->post();
        $data = (new OptService())->get_base_info('qi_dang_dangtype');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_dang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];

        if(isset($param['dangtype']) && !empty($param['dangtype'])){
//            $where[] = ['dang.dangtype','=', $param['dangtype']];
            $topid = OptionModel::where(['comid'=>$this->userinfo['comid'],'num'=>'qi_dang_dangtype','deleted_user'=>0])->value('id');
            $id = OptionModel::where(['comid'=>$this->userinfo['comid'],'topid'=>$topid,'keyid'=>$param['dangtype'],'deleted_user'=>0])->value('id');
            $ids = (new OptService())->get_option_ids($id);
            $ids[] = $param['dangtype'];
            $where[] = ['dang.dangtype','in', $ids];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['dang.number|dang.name|dang.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['dang.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['dang.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['dang.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['dangcun']) && !empty($param['dangcun'])){
            $where[] = ['dang.dangcun','=', $param['dangcun']];
        }
        if(isset($param['isjie']) && !empty($param['isjie'])){
            $where[] = ['dang.isjie','=', $param['isjie']];
        }


        $data['dangcun'] = (new OptService())->get_base_info('qi_dang_dangcun');
        $data['is_dangcun'] = isset($param['dangcun']) ? $param['dangcun'] : '';


        $data['data'] = DangModel::alias('dang')
            ->field('dang.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dang', 'dang'))
            ->order('dang.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangtype'] = (new OptService())->get_base_showname('qi_dang_dangtype', $item['dangtype']);
                $item['dangcun'] = (new OptService())->get_base_showname('qi_dang_dangcun', $item['dangcun']);


                $item['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt = '' || list.fandt is null")
                    ->where('list.dangid','=',$item['id'])
                    ->sum('list.num');

//                $item['jienum'] +=  DangJieListModel::alias('list')
//                    ->join('dang_jie jie','jie.id=list.jieid')
//                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
//                    ->where('list.dangid','=',$item['id'])
//                    ->sum('list.num');




                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dang');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_dang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dangtype'])){
                return outputJson(4000, '请选择所属分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = DangModel::insertGetId($param);
                (new OptService())->relation_file($files,'dang',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dang', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dang', $id, '档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['dangtype'] = (new OptService())->get_base_info('qi_dang_dangtype');
            $data['dangcun'] = (new OptService())->get_base_info('qi_dang_dangcun');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dang(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = DangModel::where(['id'=>$param['id']])->find();

            if(empty($param['dangtype'])){
                return outputJson(4000, '请选择所属分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                DangModel::update($param);
                (new OptService())->relation_file($files,'dang',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dang', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dang', $param['id'], '档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DangModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['dangtype'] = (new OptService())->get_base_info('qi_dang_dangtype');
            $data['dangcun'] = (new OptService())->get_base_info('qi_dang_dangcun');
            $data['files'] = (new OptService())->get_file('dang', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_dang(Request $request){
        $param = $request->post();
        $data = DangModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            DangModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'dang', $data['id'], '档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_dang(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DangModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $data['data']['dangtype'] = (new OptService())->get_base_showname('qi_dang_dangtype', $data['data']['dangtype']);
        $data['data']['dangcun'] = (new OptService())->get_base_showname('qi_dang_dangcun', $data['data']['dangcun']);

        $data['files'] = (new OptService())->get_file('dang', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_dang_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.useer|jie.depter|jie.purpose','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['useer']) && !empty($param['useer'])){
            $where[] = ['jie.useer','like', '%'.$param['useer'].'%'];
        }
        if(isset($param['depter']) && !empty($param['depter'])){
            $where[] = ['jie.depter','like', '%'.$param['depter'].'%'];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['jie.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['purpose']) && !empty($param['purpose'])){
            $where[] = ['jie.purpose','like', '%'.$param['purpose'].'%'];
        }

        if(isset($param['created_at']) && !empty($param['created_at'])){
            $where[] = ['jie.created_at','between', explode(' - ', $param['created_at'])];
        }
        if(isset($param['isjie']) && $param['isjie'] != ''){
            $where[] = ['jie.isjie','=', $param['isjie']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }


        $data['data'] = DangJieModel::alias('jie')
            ->field('jie.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dang_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['isjie_name'] = $item['isjie'] > 0 ? '已借出' : '未借出';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dang_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['guidt'])){
                return outputJson(4000, '请选择预计归还日期');
            }


            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['status'] = 1;
            $newparam['isturn'] = 1;
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            $list = array_filter($param['list']);
            unset($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入借阅档案明细');
            }

            $newparam['useer'] = $param['useer'];
            $newparam['depter'] = $param['depter'];
            $newparam['guidt'] = $param['guidt'];
            $newparam['purpose'] = $param['purpose'];
            $newparam['explain'] = $param['explain'];


            foreach($list as $key=>$vv){
                $dang = DangModel::where(['id'=>$vv['dangid']])->find();

                $vv['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');
                $vv['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');

                $vv['kejie'] = $dang['num'] - $vv['jienum'];


                if($vv['num'] > $vv['kejie']){
                    return outputJson(4000, '第'.$key.'行借阅数量已超档案可借数量');
                }
            }




            Db::startTrans();
            try {
                $id = DangJieModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    DangJieListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'dang_jie',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dang_jie', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dang_jie', $id, '档案借阅');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['useer'] = $this->userinfo['realname'];
            $data['depter'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = DangJieModel::where(['id'=>$param['id']])->find();

            if(empty($param['guidt'])){
                return outputJson(4000, '请选择预计归还日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入借阅档案明细');
            }


            $newparam['id'] = $param['id'];
            $newparam['useer'] = $param['useer'];
            $newparam['depter'] = $param['depter'];
            $newparam['guidt'] = $param['guidt'];
            $newparam['purpose'] = $param['purpose'];
            $newparam['explain'] = $param['explain'];


            foreach($list as $key=>$vv){
                $dang = DangModel::where(['id'=>$vv['dangid']])->find();

                $vv['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');
                $vv['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');

                $vv['kejie'] = $dang['num'] - $vv['jienum'];


                if($vv['num'] > $vv['kejie']){
                    return outputJson(4000, '第'.$key.'行借阅数量已超档案可借数量');
                }
            }

            Db::startTrans();
            try {
                DangJieModel::update($newparam);

                DangJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    DangJieListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'dang_jie',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dang_jie', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dang_jie', $param['id'], '档案借阅');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DangJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $list = DangJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $dang = DangModel::where(['id'=>$item['dangid']])->find();
                $item['name'] = $dang['name'];
                $item['number'] = $dang['number'];
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('dang_jie', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_dang_jie(Request $request){
        $param = $request->post();
        $data = DangJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DangJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'dang_jie', $data['id'], '档案借阅');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_dang_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DangJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $list = DangJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $dang = DangModel::where(['id'=>$item['dangid']])->find();
            $item['name'] = $dang['name'];
            $item['number'] = $dang['number'];
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('dang_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function jie_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = DangJieModel::where(['id'=>$param['id']])->find();


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入借阅档案明细');
            }

            Db::startTrans();
            try {
                DangJieModel::where(['id'=>$param['id']])->update(['isjie'=>1]);
                foreach($list as $vv){
                    DangJieListModel::update($vv);
                }

                (new OptService())->add_flow_log('借阅', 'dang_jie', $param['id']);
                (new OptService())->add_log_list('借阅', 'dang_jie', $param['id'], '档案借阅');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DangJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = DangJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $dang = DangModel::where(['id'=>$item['dangid']])->find();
                $item['name'] = $dang['name'];
                $item['number'] = $dang['number'];
                if($data['data']['isjie'] < 1){
                    $item['jiedt'] = date("Y-m-d");
                    $item['jieer'] = $this->userinfo['realname'];
                }
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function list_dang_jie_detail(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.isjie','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.useer|jie.depter|dang.name|dang.number|list.jieer|list.shouer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['useer']) && !empty($param['useer'])){
            $where[] = ['jie.useer','like', '%'.$param['useer'].'%'];
        }
        if(isset($param['depter']) && !empty($param['depter'])){
            $where[] = ['jie.depter','like', '%'.$param['depter'].'%'];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['jie.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['purpose']) && !empty($param['purpose'])){
            $where[] = ['jie.purpose','like', '%'.$param['purpose'].'%'];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['list.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['jiedt']) && !empty($param['jiedt'])){
            $where[] = ['list.jiedt','between', explode(' - ', $param['jiedt'])];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['list.jieer','=', $param['jieer']];
        }
        if(isset($param['fandt']) && !empty($param['fandt'])){
            $where[] = ['list.fandt','between', explode(' - ', $param['fandt'])];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['list.shouer','like', '%'.$param['shouer'].'%'];
        }


        $data['data'] = DangJieListModel::alias('list')
            ->field('list.*,jie.useer,jie.depter,dang.name,dang.number')
            ->join('dang_jie jie','jie.id=list.jieid')
            ->join('dang dang','dang.id=list.dangid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dang_jie_detail', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dang_jie_detail');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function fan_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入档案归还明细');
            }

            Db::startTrans();
            try {
                foreach($list as $vv){
                    DangJieListModel::update($vv);
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $ids = $request->get('ids',0,'string');

            $list = DangJieListModel::where('id','in',$ids)->select()->toArray();
            foreach ($list as &$item){
                $dang = DangModel::where(['id'=>$item['dangid']])->find();
                $item['name'] = $dang['name'];
                $item['number'] = $dang['number'];

                $jie = DangJieModel::where(['id'=>$item['jieid']])->find();
                $item['useer'] = $jie['useer'];
                if(empty($item['fandt'])){
                    $item['fandt'] = date("Y-m-d");
                }
                if(empty($item['shouer'])){
                    $item['shouer'] = $this->userinfo['realname'];
                }
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }











}
